var playState={
    create:function(){
        //capture key by phaser only,browser can't catch it
        game.input.keyboard.addKeyCapture([Phaser.Keyboard.UP,Phaser.Keyboard.DOWN,Phaser.Keyboard.LEFT,Phaser.Keyboard.RIGHT]);
        this.wasd={
            up:game.input.keyboard.addKey(Phaser.Keyboard.W),
            left:game.input.keyboard.addKey(Phaser.Keyboard.A),
            right:game.input.keyboard.addKey(Phaser.Keyboard.D)
        }

        this.nextEnemy=0;
        this.jumpSound=game.add.audio(resRef.jumpEf);
        this.coinSound=game.add.audio(resRef.cointEf);
        this.deadSound=game.add.audio(resRef.deadEf);
        //add test ef
        this.ef=[];
        this.efIndex=0;
        for(var i=0;i<7;i++){
            var str='ef'+i;
            this.ef.push(game.add.audio(str));
        }
        this.testEf=game.add.sprite(200,50,resRef.jumpButton);
        this.testEf.inputEnabled=true;
        this.testEf.events.onInputDown.add(this.switchEf,this);
        ///
        this.music=game.add.audio(resRef.music);
        this.music.loop=true;
        this.music.play();
        this.player=game.add.sprite(game.world.centerX,game.world.centerY,resRef.player);
        this.player.anchor.setTo(0,0);
        //add player animations
        this.player.animations.add('right',[1,2],8,true);
        this.player.animations.add('left',[3,4,],8,true);
        //    add gravity to player
        game.physics.arcade.enable(this.player);
        this.player.body.gravity.y=500;
        this.cursor=game.input.keyboard.createCursorKeys();
        this.createWorld();
        //    add coin
        this.coin=game.add.sprite(60,140,'coin');
        game.physics.arcade.enable(this.coin);
        this.coin.anchor.setTo(0.5,0.5);
        //this.coin.body.gravity.y=100;
        //    add score
        this.scoreLabel=game.add.text(30,30,'score:0',{
            font:'18px Arial',fill:'#fff'
        });
        game.global.score=0;
        //    Create enemy
        this.enemies=game.add.group();
        this.enemies.enableBody=true;
        this.enemies.createMultiple(10,resRef.enemy);
        //game.time.events.loop(this.nextEnemy,this.addEnemy,this);
        //create emitter
        this.emitter=game.add.emitter(0,0,15);
        this.emitter.makeParticles(resRef.pixel);
        this.emitter.setYSpeed(-150,150);
        this.emitter.setXSpeed(-150,150);
        //this.emitter.gravity=0;
        if(!game.device.desktop){
            this.addMobileInputs();
        }
    },
    switchEf:function(){
        this.ef[this.efIndex++].play();
        if(this.efIndex>=this.ef.length){
            this.efIndex=0;
        }
    },
    jumpPlayer:function(){
        if(this.player.body.onFloor()){
            this.player.body.velocity.y=-320;
            this.jumpSound.play();
        }
    },
    addEnemy:function(){
        var enemy=this.enemies.getFirstDead();
        if(!enemy){
            return ;
        }
        enemy.anchor.setTo(0.5,1);
        enemy.reset(game.world.centerX,0);
        enemy.body.gravity.y=500;
        enemy.body.velocity.x=100*Phaser.Math.randomSign();
        enemy.body.bounce.x=1;
        enemy.checkWorldBounds=true;
        enemy.outOfBoundsKill=true;
    },
    update:function(){
        game.physics.arcade.collide(this.enemies,this.layer);
        game.physics.arcade.overlap(this.player,this.enemies,this.playerDie,null,this);
        game.physics.arcade.collide(this.player,this.layer);
        game.physics.arcade.overlap(this.player,this.coin,this.takeCoin,null,this);
        //game.physics.arcade.collide(this.walls,this.coin);
        this.movePlayer();
        if(!this.player.inWorld){
            this.playerDie();
        }
        if(this.nextEnemy<game.time.now){
            var start=4000,end=1000,score=100;
            var delay=Math.max(start-(start-end)*game.global.score/score,end);
            this.addEnemy();
            this.nextEnemy=game.time.now+delay;
        }
    },
    movePlayer:function(){
        if(this.cursor.left.isDown||this.wasd.left.isDown||this.moveLeft){
            this.player.body.velocity.x=-200;
            this.player.animations.play('left');
        }else if(this.cursor.right.isDown||this.wasd.right.isDown||this.moveRight){
            this.player.animations.play('right');
            this.player.body.velocity.x=200;
        }else{
            this.player.body.velocity.x=0;
        }
        if((this.cursor.up.isDown||this.wasd.up.isDown)&&this.player.body.onFloor()){
            //this.jumpSound.play();
            //this.player.body.velocity.y=-350;
            this.jumpPlayer();
        }
    },
    addMobileInputs:function(){
        this.jumpButton=game.add.sprite(350,247,resRef.jumpButton);
        this.jumpButton.inputEnabled=true;
        this.jumpButton.alpha=0.5;

        this.leftButton=game.add.sprite(50,247,resRef.leftButton);
        this.leftButton.inputEnabled=true;
        this.leftButton.alpha=0.5;
        this.rightButton=game.add.sprite(130,247,resRef.rightButton);
        this.rightButton.inputEnabled=true;
        this.rightButton.alpha=0.5;
        this.jumpButton.events.onInputDown.add(this.jumpPlayer,this);
        this.moveLeft=false;
        this.moveRight=false;
        this.leftButton.events.onInputOver.add(function(){this.moveLeft=true;},this);
        this.leftButton.events.onInputOut.add(function(){this.moveLeft=false;},this);
        this.leftButton.events.onInputDown.add(function(){this.moveLeft=true;},this);
        this.leftButton.events.onInputUp.add(function(){this.moveLeft=false;},this);
        //add right button events
        this.rightButton.events.onInputOver.add(function(){this.moveRight=true;},this);
        this.rightButton.events.onInputDown.add(function(){this.moveRight=true;},this);
        this.rightButton.events.onInputOut.add(function(){this.moveRight=false;},this);
        this.rightButton.events.onInputUp.add(function(){this.moveRight=false;},this);

    },
    createWorld:function(){
        //this.walls=game.add.group();
        //this.walls.enableBody=true;
        //game.add.sprite(0,0,resRef.vertical_wall,0,this.walls);
        //game.add.sprite(480,0,resRef.vertical_wall,0,this.walls);
        //game.add.sprite(0,0,resRef.horizontal_wall,0,this.walls);
        //game.add.sprite(300,0,resRef.horizontal_wall,0,this.walls);
        //game.add.sprite(0,320,resRef.horizontal_wall,0,this.walls);
        //game.add.sprite(300,320,resRef.horizontal_wall,0,this.walls);
        //game.add.sprite(-100,160,resRef.horizontal_wall,0,this.walls);
        //game.add.sprite(400,160,resRef.horizontal_wall,0,this.walls);
        //
        //var middleTop=game.add.sprite(100,80,resRef.horizontal_wall,0,this.walls);
        //middleTop.scale.setTo(1.5,1);
        //var middleBottom=game.add.sprite(100,240,resRef.horizontal_wall,0,this.walls);
        //middleBottom.scale.setTo(1.5,1);
        //this.walls.setAll('body.immovable',true);
        this.map=game.add.tilemap(resRef.map);
        this.map.addTilesetImage(resRef.tileset);
        this.layer=this.map.createLayer('Tile Layer 1');
        this.layer.resizeWorld();
        this.map.setCollision(1);
    },
    startMenu:function(){
        game.state.start('menu');
    },
    playerDie:function(){
        //when player fall into hole,this function will called many times,so
        if(!this.player.alive){
            return;
        }
        this.music.stop();
        this.emitter.x=this.player.x;
        this.emitter.y=this.player.y;
        this.emitter.start(true,600,null,15);
        this.player.kill();
        this.deadSound.play();
        game.time.events.add(1000,this.startMenu,this);
    },
    takeCoin:function(){
        this.coinSound.play();
        this.updateCoinPosition();
        //player becomes bigger for a moment,when take a coin.
        game.add.tween(this.player.scale).to({x:1.3,y:1.3},50).to({x:1,y:1},150).start();
        game.global.score+=5;
        this.scoreLabel.text='score: '+game.global.score;
        //coin appear with scaling
        this.coin.scale.setTo(0);
        game.add.tween(this.coin.scale).to({x:1,y:1},300).start();
    },
    updateCoinPosition:function(){
        var coinPosition=[
            {x:140,y:60},{x:360,y:60},
            {x:60,y:140},{x:440,y:140},
            {x:130,y:300},{x:370,y:300}
        ];
        for(var i=0;i<coinPosition.length;i++){
            if(coinPosition[i].x===this.coin.x){
                coinPosition.splice(i,1);
            }
        }
        var newPosition=coinPosition[
            game.rnd.integerInRange(0,coinPosition.length-1)
            ];
        this.coin.reset(newPosition.x,newPosition.y);
    }
};
